
#import "TRManager.h"
#import "FMDB.h"

@implementation TRManager
{
    FMDatabaseQueue * _queue;
    FMDatabase * _dataBase;
}
#pragma mark - 单例对象
+ (TRManager *)manager{
    
    static TRManager * manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[TRManager alloc] init];
    });
    return manager;
}

- (id)init{
    
    if (self = [super init]) {
        _queue = [FMDatabaseQueue databaseQueueWithPath:[NSString stringWithFormat:@"%@/%@",db_path,db_name]];
        _dataBase = [[FMDatabase alloc] initWithPath:[NSString stringWithFormat:@"%@/%@",db_path,db_name]];
    }
    return self;
}

#pragma mark - 创建详细数据表
- (void)createTable:(NSString *)tableName andSql:(NSString *)sql isSuccess:(IsSuccBlock)isSuccBlock{
        
    NSString * createSql = [NSString stringWithFormat:@"create table if not exists %@(%@)",tableName,sql];
    [self executeUpdate:createSql isSuccess:isSuccBlock];
}

- (void)createTable:(NSString *)tableName andSql:(NSString *)sql{
    
    NSString * createSql = [NSString stringWithFormat:@"create table if not exists %@(%@)",tableName,sql];
    if ([_dataBase open]) {
        [_dataBase executeUpdate:createSql];
    }
}

#pragma mark - 查询数据
- (void)selectDataWithSql:(NSString *)sql finished:(void(^)(FMResultSet *result))finished{
    
    NSString * selectSql = [NSString stringWithFormat:@"select * from %@",sql];
    [self executeQuery:selectSql finished:finished];
}

- (FMResultSet *)selectDataWithSql:(NSString *)sql{
    
    NSString * selectSql = [NSString stringWithFormat:@"select * from %@",sql];
    if ([_dataBase open]) {
        FMResultSet * rs = [_dataBase executeQuery:selectSql];
        return rs;
    }
    return nil;
}

#pragma mark - 插入数据
- (void)insertDataWithSql:(NSString *)sql isSuccess:(IsSuccBlock)isSuccBlock{
    
    NSString * insertSql = [NSString stringWithFormat:@"insert into %@",sql];
    [self executeUpdate:insertSql isSuccess:isSuccBlock];
}

#pragma mark - 删除数据
- (void)deleteDataWithSql:(NSString *)sql isSuccess:(IsSuccBlock)isSuccBlock{
    
    NSString * deleteSql = [NSString stringWithFormat:@"delete from %@",sql];
    [self executeUpdate:deleteSql isSuccess:isSuccBlock];
}

#pragma mark - 修改数据
- (void)updateDataWithSql:(NSString *)sql isSuccess:(IsSuccBlock)isSuccBlock{
    
    NSString * updateSql = [NSString stringWithFormat:@"update %@",sql];
    [self executeUpdate:updateSql isSuccess:isSuccBlock];
}

- (void)executeUpdate:(NSString *)sql{
    
    if ([_dataBase open]) {
        [_dataBase executeUpdate:sql];
    }
}

- (void)executeQuery:(NSString *)sql finished:(void(^)(FMResultSet *result))finished{
    
    [self dispatch_async:^(FMDatabase *db) {
        
        FMResultSet * rs = [db executeQuery:sql];
        finished(rs);
    }];
}

- (void)executeUpdate:(NSString *)sql isSuccess:(IsSuccBlock)isSuccBlock{
    
    [self dispatch_async:^(FMDatabase *db) {
        
        if ([db executeUpdate:sql]) {
            isSuccBlock(YES);
        }else{
            isSuccBlock(NO);
        }
    }];
}

- (FMResultSet *)executeQuery:(NSString *)sql{
    
    if ([_dataBase open]) {
        return [_dataBase executeQuery:sql];
    }
    return nil;
}

- (void)dispatch_async:(void(^)(FMDatabase *db))block{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        [_queue inDatabase:^(FMDatabase *db) {
            block(db);
        }];
    });
}

@end
